18. POM Inheritance

Pom Inheritance and Multiple Module Projects

ND079 JPND C3 L2 A14 Pom Inheritance And Multiple Module Projects V3

Pom Inheritance

Just like all classes in Java inherit from the Object class, all poms in Maven inherit from the Super Pom. The Super Pom contains the default settings used by Maven, and you can override them with settings in your pom. You can see the Super Pom in Maven's documentation.

ND079 JPND C3 L2 A15 Demo Pom Inheritance And Multiple Module Projects

You can also create an inheritance hierarchy among your own poms. First, you have to define a pom to be used as a parent by specifying the pom <packaging> type.

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.udacity.jpnd</groupId>
  <artifactId>maven-test-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>com.udacity.jpnd</groupId>
      <artifactId>D</artifactId>
    </dependency>
  </dependencies>
</project>

Then you create a pom that inherits from that pom using the <parent> tag.

<project>
  <parent>
    <groupId>com.udacity.jpnd</groupId>
    <artifactId>maven-test-parent</artifactId>
    <version>1.0.0</version>  
  </parent>
  <groupId>com.udacity.jpnd</groupId>
  <artifactId>B</artifactId>
  <version>1.0.0</version>
</project>

Multi-Module Projects

To create a Maven project containing multiple modules, create a <modules> tag at the top level of the pom.

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.udacity.jpnd</groupId>
  <artifactId>maven-test-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>B</module>
    <module>C</module>
  </modules>

  <dependencies> ... </dependencies>
</project>